﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using nyxwin.Main.ViewModel.SubModels;
using Microsoft.Phone.Shell;
using nyxwin.Common.Providers;
using nyxwin.Main.Base.Interfaces;

namespace nyxwin.Main.View.SubViews
{
    public partial class NewMail : PhoneApplicationPage, ISupportErrors, ISupportProgress
    {
        private NewMailViewModel _model;
        private ApplicationBarIconButton _sendButton;
        private ApplicationBarIconButton _cancelButton;

        public NewMail()
        {
            InitializeComponent();

            this._model = new NewMailViewModel();
            this.DataContext = this._model;

            this.Unloaded += new RoutedEventHandler(NewMail_Unloaded);

            _sendButton = new ApplicationBarIconButton();
            _sendButton.IconUri = new Uri("/Icons/mail.png", UriKind.Relative);
            _sendButton.Click += new EventHandler(_sendButton_Click);
            _cancelButton = new ApplicationBarIconButton();
            _cancelButton.IconUri = new Uri("/Icons/cancel.png", UriKind.Relative);
            _cancelButton.Click += new EventHandler(_cancelButton_Click);
            ActualizeButtonTitles();

            this.ApplicationBar.Buttons.Add(_sendButton);
            this.ApplicationBar.Buttons.Add(_cancelButton);

            this._model.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(_model_PropertyChanged);
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (NavigationContext.QueryString.ContainsKey("nick"))
            {
                var nick = NavigationContext.QueryString["nick"];
                this._model.Recipient = nick;
            }
        }

        void _cancelButton_Click(object sender, EventArgs e)
        {
            this._model.Cancel();
        }

        void _sendButton_Click(object sender, EventArgs e)
        {
            // NOTE: Hack for focus bug in WP7
            object focusObj = FocusManager.GetFocusedElement();

            if (focusObj != null && focusObj is TextBox)
            {

                var binding = (focusObj as TextBox).GetBindingExpression(TextBox.TextProperty);

                binding.UpdateSource();

            }

            this._model.Send();
        }

        void _model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName.Contains("ButtonTitle"))
            {
                ActualizeButtonTitles();
            }
        }

        private void ActualizeButtonTitles()
        {
            _sendButton.Text = this._model.SendButtonTitle;
            _cancelButton.Text = this._model.CancelButtonTitle;
        }

        void NewMail_Unloaded(object sender, RoutedEventArgs e)
        {
            this._model.Unload();
        }

        #region ISupportErrors Members

        public void RaiseError(string message)
        {
            MessageBox.Show(message, LocalizationProvider.Instance.GetString("Error"), MessageBoxButton.OK);
        }

        #endregion

        #region ISupportProgress Members

        public void SetInDeterminate(Guid id, string text)
        {
            Overlay.Visibility = System.Windows.Visibility.Visible;
            _sendButton.IsEnabled = false;
            _cancelButton.IsEnabled = false;
            progressText.Text = text;
        }

        public void UnSetInDeterminate(Guid id)
        {
            Overlay.Visibility = System.Windows.Visibility.Collapsed;
            _sendButton.IsEnabled = true;
            _cancelButton.IsEnabled = true;
        }

        #endregion
    }
}